<template>
    <div>
        <a-form :form="form" >
        <a-row :gutter="48">
            <a-col :sm="{span:12}" :lg="{span:6}" :xl="{span:6}" >
            <a-form-item label="院系">
              <a-select showSearch optionFilterProp="children" v-decorator="[
                    'ssyxm',
                    {
                        rules: [{ required: true, message: '请选择院系!' }],
                    },
                    {initialValue:postParams.ssyxm}
                    ]" disabled @change='yxChange'>
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+"-"+ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:6}" :xl="{span:6}">
            <a-form-item label="专业">
              <a-select showSearch optionFilterProp="children" v-decorator="[
                    'sszym',
                    {
                        rules: [{ required: true, message: '请选择专业!' }],
                    },
                    {initialValue:postParams.sszym}
                    ]" :disabled="disabledStop" @change='zyChange'>
                <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh+"-"+i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col ::sm="{span:12}" :lg="{span:6}" :xl="{span:6}">
            <a-form-item label="招生人数">
              <a-input  v-decorator="[
                    'zsrs',
                    {
                        rules: [{ required: true, message: '请输入招生人数!' }],
                    },
                    {initialValue:postParams.zsrs}
                    ]" :disabled="disabledStop" />
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:6}" :xl="{span:6}">
            <a-form-item label="招生年度">
              <a-select showSearch optionFilterProp="children" disabled v-decorator="[
                    'zsxn' ,
                    {
                        rules: [{ required: true, message: '请选择招生年度!' }],
                    },
                    {initialValue:postParams.zsxn}
                    ]"  >
                <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="24" :md="24" :sm="24">
              <a-form-item
                  label="考试科目"
              >
                  <a-textarea  v-decorator="[
                  'kskm',
                  {
                      rules: [{ required: true, message: '请输入考试科目!' }],
                  },
                  {initialValue:postParams.kskm}
                  ]" disabled rows="4"/>
              </a-form-item>
          </a-col>
            <a-col :lg="24" :md="24" :sm="24">
                <a-form-item
                    label="复试科目"
                >
                    <a-textarea  v-decorator="[
                    'fskm',
                    {

                    },
                    {initialValue:postParams.fskm}
                    ]" :disabled="disabledStop" rows="4"/>
                </a-form-item>
            </a-col>
            <a-col :lg="24" :md="24" :sm="24">
                <a-form-item
                    label="加试科目"
                >
                    <a-textarea  v-decorator="[
                    'jskm',
                    {

                    },
                    {initialValue:postParams.jskm}
                    ]" :disabled="disabledStop" rows="4"/>
                </a-form-item>
            </a-col>
            <a-col :lg="24" :md="24" :sm="24">
                <a-form-item
                    label="备注"
                >
                    <a-textarea  v-decorator="[
                    'note',
                    {

                    },
                    {initialValue:postParams.note}
                    ]" :disabled="disabledStop" rows="4" />
                </a-form-item>
            </a-col>
        </a-row>
        </a-form>
    </div>
</template>

<script>
  import {mapState} from 'vuex'

  const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072']

export default {
  props: {
    confirmHandleVisiable: {
      default: false
    },
    info: {
      default: () => ({})
    },
    // 是否禁止编辑
    disabledStop: {
      default: true
    },
      keyNum:{
         default:""
      }
  },
  data () {
    return {
      loading: false,
      selectMenu: {
        yx: [] ,// 院系
        zy: [] //专业
      },
      postParams:{},
      from:''
    }
  },
  beforeCreate () {
    this.form = this.$form.createForm(this)
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0321: state => state.app['TYPE0321'], // 是否在岗
      TYPE0126: state => state.app['TYPE0126'], // 年级
      TYPE0011: state => state.app['TYPE0011'] // 性别
    })
  },
  created () {
    // selectArr.forEach(v => {
    //   this.$store.dispatch('app/setDictionary', v)
    // })
     this.postParams = {}
    this.form.resetFields();
    this.postParams = this.info

    if(this.info){
    for (let key in this.info) {
      if(key == 'ssyxm' || key == 'sszym' || key == 'yjfxm' || key=='zsrs' || key=='fskm' || key=='jskm' || key=='zsxn' || key == 'id' || key == 'note'){
        this.postParams[key] = this.info[key]
        this.form.getFieldDecorator(key, {initialValue: this.info[key]})
      }else if(key = 'kskm'){
        this.postParams[key] = this.info['kskmmc']
        this.form.getFieldDecorator(key, {initialValue: this.info['kskmmc']})
      }
    }
    }
    this.initSelectMenu()
  },

  watch:{
    keyNum(){
      this.startFun()
    },
      info(){
      this.startFun()
    }
  },
  methods: {
    startFun(){
      this.postParams ={}
       // this.form.setFieldsValue({})

       this.form.resetFields()
      // if(!this.info['fskm']) this.info['fskm'] = ''
      // if(!this.info['jskm']) this.info['jskm'] = ''
      // if(!this.info['note']) this.info['note'] = ''


      for (let key in this.info) {
        if(key == 'ssyxm' || key == 'sszym' || key == 'yjfxm' || key=='zsrs' || key=='fskm' || key=='jskm' || key=='zsxn' || key == 'id' || key == 'note'){
          this.postParams[key] = this.info[key]
          // this.$set(this.postParams,key,this.info[key])
         this.form.getFieldDecorator(key, {initialValue: this.info[key]})
        }else if(key=='kskm'){
           this.form.getFieldDecorator(key, {initialValue: this.info['kskmmc']})
        }
      }
      this.initSelectMenu()
    },
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        if(!this.info.ssyxm){
          this.form.getFieldDecorator('ssyxm',{initialValue:res[0].data[0].dwh})
          this.getProssion(res[0].data[0].dwh)
        }else{
           this.getProssion(this.info.ssyxm)
        }
        // 获取当前学年
        this.$api.base.getSchoolYear().then(res=>{
          if(!this.info.zsxn){
            // this.info.zsxn = res.data.masterAdmissionYear
            this.$set(this.postParams,'zsxn',res.data.masterAdmissionYear)
            console.log(this.postParams.zsxn)
            this.form.getFieldDecorator('zsxn',{initialValue:res.data.masterAdmissionYear})
          }else{
             this.form.getFieldDecorator('zsxn', {initialValue: this.info['zsxn']})
          }
        })
      })
    },
    yxChange(code){
      this.form.setFieldsValue({'kskm':''})
      this.form.setFieldsValue({'sszym':''})
      this.$set(this.selectMenu, 'zy', [])
      if(code){
        this.getProssion(code)
      }
    },
    // 获取专业
    getProssion(code){
         // 获取专业
      this.$api.base.majorAll({
        lsdwh: code,zycc:'2'
      }).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
    },
    zyChange(code){
      this.form.setFieldsValue({'kskm':''})
      // if(code){
        this.getbskm(code)
      // }
    },
    getbskm(code){
      var params = {
        ssyxm:this.form.getFieldValue('ssyxm'),
        sszym:code,
        zsxn:this.form.getFieldValue('zsxn')
      }
      if(params.ssyxm && params.sszym && params.zsxn){
         this.$api.base.getClassKskm(params).then(res => {
          this.form.setFieldsValue({'kskm':res.data})
        })
      }else{
        this.form.setFieldsValue({'kskm':''})
      }

    },
    // 关闭
    onClose () {
       this.postParams ={}
      // this.$emit('close')
      this.form.resetFields()
      this.initSelectMenu()
    },
    // 验证数据并输出
    GetInfo () {
      this.form.validateFields( (err,values) => {
        if(err){
          this.$message.error("请选择必填项")
        }else{
          console.log(this.info)
          if(!this.info || !this.info.zsSsszszyml || !this.info.zsSsszszyml.id){
            this.$api.recruitstat.setUpBySpecialtyFirstAdd(this.form.getFieldsValue()).then(res => {
              var newData = Object.assign(res.data,this.form.getFieldsValue())
              console.log('1',newData)
              // return newData
              this.form.resetFields()
              this.$emit('getInfoValue',newData)
            }).finally(() => {
              this.loadingBtn = false
            })
          }else{
            var params =''
            params = Object.assign({'id':this.info.zsSsszszyml.id},this.form.getFieldsValue())
            console.log(this.form.getFieldsValue())
             this.$api.recruitstat.setUpBySpecialtyFirstUpdate(params).then(res => {
              console.log('2',params)
              this.form.resetFields()
              this.$emit('getInfoValue',params)
            }).finally(() => {
              this.loadingBtn = false
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="less">
.fullscreen{
    .ant-drawer-wrapper-body{
        height: 100%;
        overflow: auto;
    }
}
</style>
